<template>
  <div class="pages_content table">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main style="display: flex">
        <div>
          <div class="header_right" @click="showCardNav = !showCardNav">
            <div class="title">搜索</div>
            <div>
              <transition name="from"></transition>
              <span v-show="!showCardNav">
                <i style="margin-right: 5px" class="el-icon-arrow-down"></i>
              </span>
              <transition name="to"></transition>
              <span v-show="showCardNav">
                <i style="margin-right: 5px" class="el-icon-arrow-up"></i>
              </span>
            </div>
          </div>
          <div v-show="showCardNav" class="hr"></div>
          <el-collapse-transition>
            <div v-show="showCardNav">
              <div class="search">
                <span style="display: inline-block;margin-bottom: 10px;">
                  <span style="margin-right: 12px">站点</span>
                  <el-select v-model="tableParam.stationId" style="width: 180px" placeholder="请选择">
                    <el-option v-for="(item,index) in pointList" :key="index" :label="item.name" :value="item.id"></el-option>
                  </el-select>
                </span>
                <span style="margin-left: 24px">
                  <span style="margin-right: 12px">特材成品名称</span>
                  <el-input v-model="tableParam.productName" style="width: 180px" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
                </span>
                <span style="margin-left: 24px">
                  <span style="margin-right: 12px">成品型号</span>
                  <el-input v-model="tableParam.productTpye" style="width: 180px" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
                </span>
                <span style="margin-left: 24px">
                  <span style="margin-right: 12px">状态</span>
                  <el-select v-model="tableParam.dataStatus" placeholder="请选择" style="width: 100px">
                    <el-option label="全部" value=""></el-option>
                    <el-option label="待处理" :value="0"></el-option>
                    <el-option label="已处理" :value="1"></el-option>
                    <el-option label="已完成" :value="2"></el-option>
                  </el-select>
                </span>
                <el-button icon="el-icon-search" size="medium" class="bottom_all" @click="(tableParam.page = 1), gettableData()">查 询</el-button>
                <el-button size="medium" class="bottom_all_two" style="margin-left: 12px" @click="init()">重 置</el-button>
              </div>
            </div>
          </el-collapse-transition>
          <div class="hr"></div>
          <div class="card">
            <div class="card_nav">
              <div>
                <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='TIANJIA'))!==-1" size="small" class="bottom_all" style="margin-left: 0" @click="productList=[],addFromDate('添加')">
                  <i class="icon iconfont iconicon_button_add" style="font-size: 12px"></i>
                  添加
                </el-button>
              </div>
              <div>
                <el-dropdown :hide-on-click="false" trigger="click" style="margin: 0 20px">
                  <el-link :underline="false">
                    <span class="el-icon--left">
                      <i class="iconfont iconicon_button_filtrate"></i>
                    </span>
                    筛选
                  </el-link>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item v-for="(item, index) in table" :key="index">
                      <el-checkbox v-model="item.status">{{
                        item.label
                        }}</el-checkbox>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </div>
            </div>
            <div v-loading="tableLoading" class="card_info" :style="style">
              <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width: 100%" :data="tableData" tooltip-effect="dark">
                <template v-for="(item, index2) in table">
                  <el-table-column v-if="item.status" :key="index2" :label="item.label" align="center" :min-width="item.width" :fixed="item.label =='操作'?'right':false">
                    <template slot-scope="scope">
                      <div v-if="item.label == '操作'">
                        <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='XIUGAI'))!==-1" class="bottom_one" size="mini" @click="updateFromDate(scope.row, '修改' ),productListFun0()">

                          修改
                        </el-button>
                        <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='PAICHE'))!==-1" class="bottom_one" size="mini" @click="open('addDialog1',scope.row)">
                          派车
                        </el-button>
                        <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='FANLIAOYUNSHU'))!==-1" class="bottom_one" size="mini" @click="open('addDialog2',scope.row)">
                          返料运输
                        </el-button>
                      </div>
                      <div v-else-if="item.label == '状态'">
                        <span v-if="scope.row.dataStatus == 2" style="color: #969799">已完成</span>
                        <span v-else-if="scope.row.dataStatus == 1" style="color: #00a754">已处理</span>
                        <span v-else style="color: red">待处理</span>
                      </div>
                      <span v-else>{{ scope.row[item.prop] }}</span>
                    </template>
                  </el-table-column>
                </template>
              </el-table>
            </div>
            <div class="card_bottom">
              <div class="card_bottom_left">
                <div class="top_page_totle">总共{{ total }}条</div>
                <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" layout="prev, pager, next, jumper" :total="total" @current-change="gettableData()"></el-pagination>
              </div>
              <div>
                <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="gettableData">刷新</el-button>
              </div>
            </div>
          </div>
        </div>
      </el-main>
    </el-container>
    <!-- 添加 -->
    <el-dialog v-dialogDrag :visible.sync="addDialog" width="480px" :close-on-click-modal="false" :before-close="resetFromDate">
      <span slot="title" class="dialog-title">{{ addDialogTitle }}</span>
      <div class="dialoginfo">
        <el-form ref="reffromDate" :rules="rules" :inline="true" :model="fromDate" label-width="110px">
          <el-form-item prop="stationId">
            <span slot="label" style="color: #ff0000">站点</span>
            <el-select ref="point" v-model="fromDate.stationId" placeholder="请选择" @change="$nextTick(()=>{fromDate.station = $refs.point.selectedLabel}),productListFun(fromDate.stationId)">
              <el-option v-for="(item,index) in pointList" :key="index" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="productId">
            <span slot="label" style="color: #ff0000">特材成品</span>
            <el-select :key="fromDate.productId" v-model="fromDate.productId" placeholder="请选择" filterable>
              <el-option v-for="(item, index) in productList" :key="index" :label="item.productName+'-'+item.type" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="returnNum">
            <span slot="label">返料量</span>
            <el-input v-model="fromDate.returnNum" type="number" placeholder="请输入" clearable>
              <template slot="append">吨</template>
            </el-input>
          </el-form-item>
          <el-form-item prop="remark">
            <span slot="label">备注</span>
            <el-input v-model="fromDate.remark" placeholder="请填写备注" class="textarea" type="textarea" resize="none" rows="5"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resetFromDate">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px" @click="pagessubmitFromDate()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 派车 -->
    <el-dialog v-dialogDrag :visible.sync="addDialog1" width="480px" :close-on-click-modal="false">
      <span slot="title" class="dialog-title">派车</span>
      <div class="dialoginfo">
        <el-form ref="reflow" :rules="rules1" :inline="true" :model="low" label-width="110px">
          <el-form-item prop="returnTime">
            <span slot="label" style="color: #ff0000">返料时间</span>
            <el-date-picker v-model="low.returnTime" type="datetime" placeholder="选择日期时间" value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-form-item>
          <el-form-item prop="remark">
            <span slot="label">备注</span>
            <el-input v-model="low.remark" placeholder="请输入" clearable> </el-input>
          </el-form-item>
        </el-form>
        <div style="padding-bottom:15px">
          <el-button size="mini" class="bottom_all" style="margin-left: 0" @click="additem()">
            <i class="icon iconfont iconicon_button_add" style="font-size: 12px"></i>
            添加
          </el-button>
        </div>
        <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width: 100%" :data="low.list" tooltip-effect="dark" :row-class-name="className">
          <el-table-column label="司机" align="center">
            <template slot-scope="scope">
              <el-select ref="driver" v-model="scope.row.driverId" placeholder="请选择" style="width:100%" @change="$nextTick(()=>{scope.row.driver = $refs.driver.selectedLabel})">
                <el-option v-for="(item, index) in userList" :key="index" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="车牌号" align="center">
            <template slot-scope="scope">
              <el-select v-model="scope.row.carNo" placeholder="请选择" style="width:100%">
                <el-option v-for="(item, index) in carList" :key="index" :value="item.carNo"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="left">
            <template slot-scope="scope">
              <el-button size="mini" class="bottom_two" :disabled="scope.row.dataStatus!=0" @click="delitem(scope.row,scope.$index)">
                <i class="icon iconfont iconicon_button_delete" style="font-size: 12px"></i>
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="reset('addDialog1')">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px" @click="submit1()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 返料运输 -->
    <el-dialog v-dialogDrag :visible.sync="addDialog2" width="980px" :close-on-click-modal="false">
      <span slot="title" class="dialog-title">返料运输</span>
      <div style="height:470px;">
        <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width: 100%;height:428px" :data="tableData1" tooltip-effect="dark">
          <el-table-column label="运输编号" align="center" prop="code"> </el-table-column>
          <el-table-column label="站点" align="center" prop="station"> </el-table-column>
          <el-table-column label="司机" align="center" prop="driver"> </el-table-column>
          <el-table-column label="车牌号" align="center" prop="carNo"> </el-table-column>
          <el-table-column label="第一次过磅量" align="center" prop="firstWeighing"> </el-table-column>
          <el-table-column label="第二次过磅量" align="center" prop="secondWeighing"> </el-table-column>
          <el-table-column label="入库量" align="center" prop="receipt"> </el-table-column>
          <el-table-column label="成品罐编号" align="center" prop="tankNo"> </el-table-column>
          <el-table-column label="成品罐名称" align="center" prop="tankName"> </el-table-column>
          <el-table-column label="状态" align="center" prop="dataStatus">
            <template slot-scope="scope">
              {{scope.row.dataStatus==0?'待处理':scope.row.dataStatus==1?'返料中':'已入库'}}
            </template>
          </el-table-column>
          <el-table-column label="入库时间" align="center" prop="storageTime"> </el-table-column>
          <el-table-column label="备注" align="center" prop="remark"> </el-table-column>
        </el-table>
        <div class="pagination">
          <div class="top_page_totle">总共{{ total2 }}条</div>
          <el-pagination :current-page.sync="from3.page" :page-size="from3.pageSize" layout="prev, pager, next, jumper" :total="total2" @current-change="getarr3()"></el-pagination>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="reset('addDialog2')">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import mixin from "@/mixins/ICPSmixin.js";
import {
  queryPage,
  saveOrUpdate,
  productList,
  carList,
  sendCar,
  shipping
} from "@/api/smart/warehouse/ADRN.js";
import { getUserAut, get_simple_organization, get_simple_user_listBycompanyId } from '@/api/common.js';
export default {
  mixins: [mixin],
  data() {
    return {
      /*表格增删改查及下载接口*/
      queryPage: queryPage,
      saveOrUpdate: saveOrUpdate,
      /*表格数据*/
      table: [
        { label: "返料编号", width: "120", prop: "code", status: true },
        { label: "站点", width: "120", prop: "station", status: true },
        { label: "特材库名称", width: "120", prop: "productName", status: true },
        { label: "成品型号", width: "120", prop: "productType", status: true },
        { label: "返料量（吨）", width: "120", prop: "returnNum", status: true },
        { label: "预计返料时间", width: "160", prop: "returnTime", status: true },
        { label: "备注", width: "300", prop: "remark", status: true },
        { label: "状态", width: "120", prop: "", status: true },
        { label: "操作", width: "280", prop: "", status: true },
      ],
      pagesType: 0,
      /*添加表单数据*/
      rules: {
        stationId: [{ required: true, message: "请选择站点", trigger: "change" }],
        productId: [{ required: true, message: "请选择特材成品", trigger: "change" }],
        returnNum: [{ required: true, message: "请输入返料量", trigger: "blur" }],
      },
      rules1: {
        returnTime: [{ required: true, message: "请选择返料时间", trigger: "change" }]
      },
      fromDate: {
        code: "",
        productId: '',
        returnNum: '',
        remark: '',
      },
      addDialog1: false,
      addDialog2: false,
      /**弹窗原始数据*/
      low: { list: [] },
      /**弹窗的新增修改数据*/
      lowData: {
      },
      /**弹窗的分页*/
      from3: {
        page: 1,
        pageSize: 12,
      },
      /**弹窗的总数*/
      total2: 0,
      /**弹窗的表格*/
      tableData1: [],
      pointList: [],
      productList: [],
      carList: [],
      userList: []
    };
  },
  created() {
    /*存储拷贝的原初始化fromDate数据*/
    this.tableParam = { ...this.tableParam, productName: "", productTpye: "", dataStatus: '', stationId: '' };
    getUserAut().then(res => {
      this.pointList = res.data.pointList
    })
    carList({ page: 1, pageSize: 9999999, enable: 1 }).then(res => {
      this.carList = res.data.data
    })
    this.gettableData();
  },
  deactivated() {
    this.addDialog = false
  },
  methods: {
    productListFun0() {
      productList({ page: 1, pageSize: 9999999, enable: 1, dataType: 2, companyId: this.fromDate.companyId }).then(res => {
        if (res.data.data.length == 0) {
          this.$message({
            message: '该站点公司无特材成品',
            type: 'warning'
          });
        }
        this.productList = res.data.data
      })
    },
    productListFun(id) {
      this.fromDate.productId = ''
      get_simple_organization({ id: id }).then((res) => {
        this.fromDate.companyId = res.data.id
        this.fromDate.company = res.data.name//查询站点所属公司
        this.productListFun0()
      });
    },
    className({ row }) {
      if (row.status == 0) {
        return 'tableHidden'
      }
    },
    additem() {
      this.low.list.push({
        driver: "",//司机
        driverId: "",//司机id
        carNo: "",//车牌号}]
        dataStatus: 0,
      })
    },
    delitem(low, index) {
      if (low.id) {
        low.status = 0
      } else {
        this.low.list.splice(index, 1)
      }

    },
    open(key, low) {
      this[key] = true
      this.low = this.copy(low)
      if (key == "addDialog1") {
        this.low.remark = ''
        get_simple_user_listBycompanyId({ roleId: '87b62423cc0c4a1c9a775a08d9bf29e4', orgznizationId: low.companyId }).then(res => {
          this.userList = res.data
        })
        this.low.list = []
        shipping({ needId: low.id }).then(res => {
          this.low.list = res.data.data
          if (res.data.data.length > 0) {
            this.low.remark = res.data.data[0].remark
          }
        })
        setTimeout(() => {
          this.$refs.reflow.clearValidate();
        }, 10)

      }
      if (key == "addDialog2") {
        this.from3.needId = low.id
        this.getarr3()
      }
    },
    getarr3() {
      shipping(this.from3).then(res => {
        this.tableData1 = res.data.data
        this.total2 = res.data.totalRows
      })
    },
    reset(key) {
      this[key] = false
    },
    /*重置数据,此方法必须存在，并且方发只重置条件*/
    init() {
      this.tableParam.productName = "";
      this.tableParam.productTpye = "";
      this.tableParam.dataStatus = "";
      this.tableParam.stationId = "";
      this.gettableData();
    },
    submit1() {
      let _this = this;
      _this.$refs.reflow.validate(async (valid) => {
        if (valid) {
          if (this.low.list.length == 0) {
            _this.$message({
              message: "请填写派车信息",
              type: 'warning'
            });
            return
          }
          if (_this.low.list.every(item => (item.driverId == '' || item.carNo == ''))) {
            _this.$message({
              message: "请填写信息完整",
              type: 'warning'
            });
            return
          }
          let data = {
            id: this.low.id,
            company: this.low.company,
            companyId: this.low.companyId,
            station: this.low.station,
            stationId: this.low.stationId,
            productId: this.low.productId,
            productName: this.low.productName,
            productType: this.low.productType,
            remark: this.low.remark,
            returnTime: this.low.returnTime,
            list: this.low.list,
          }
          sendCar(data).then(res => {
            if (res.code == 1001) {
              _this.$message({
                message: '保存成功',
                type: "success",
              });
              _this.gettableData();
              _this.reset('addDialog1')
            }
          })
          console.log(123)
        } else {
          return false;
        }
      });
    },
    pagessubmitFromDate() {
      let _this = this;
      _this.$refs.reffromDate.validate(async (valid) => {
        if (valid) {
          const { data } = await get_simple_organization({ id: _this.fromDate.stationId })
          _this.fromDate.company = data.name
          _this.fromDate.companyId = data.id
          let item = _this.productList.find(res => (res.id == _this.fromDate.productId))
          _this.fromDate.productName = item.productName
          _this.fromDate.productType = item.type
          _this.$router.push({ name: "sign_ADRN", query: { id: JSON.stringify(_this.fromDate), type: 1 } });
          //_this.submitFromDate();
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input,
.el-select,
.el-textarea {
  width: 240px;
}
/*
 <el-link class="table_link" :underline="false">查看配合比</el-link>*/
.table_link {
  margin: 0 10px;
  color: $bg_color;
  cursor: pointer;
}

.table_link:hover {
  opacity: 0.8;
}
</style>
<style>
.tableHidden {
  display: none;
}
</style>

